package com.gdufe.mistakebook.service.impl;

import com.gdufe.mistakebook.entity.Label;
import com.gdufe.mistakebook.entity.dto.LabelDto;
import com.gdufe.mistakebook.entity.vo.QueryLabelsVo;
import com.gdufe.mistakebook.exception.BusinessException;
import com.gdufe.mistakebook.mapper.LabelMapper;
import com.gdufe.mistakebook.result.PageResult;
import com.gdufe.mistakebook.service.LabelService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
* @author hoshino
* @description 针对表【label】的数据库操作Service实现
* @createDate 2024-03-20 13:20:18
*/
@Service
public class LabelServiceImpl implements LabelService{

   @Resource
   private LabelMapper labelMapper;


    @Override
    public PageResult pageQuestions(Map<String, String> params) {

        Integer current = Integer.parseInt(params.get("page"));
        Integer pageSize = Integer.parseInt(params.get("limit"));
        Integer userId = Integer.parseInt(params.get("userId"));
        String key = params.get("key").toString();
        if (current == null || pageSize == null) {
            throw new BusinessException(0, "分页参数不能为空");
        }
        if (current <= 0 || pageSize < 0) {
            throw new BusinessException(0, "分页参数异常");
        }
        //开启分页
        PageHelper.startPage(current, pageSize);
        Page<QueryLabelsVo> page = labelMapper.QueryLabels(userId,key);
        List<QueryLabelsVo> result = page.getResult();
        //判空
        if (CollectionUtils.isEmpty(result)) {
            return new PageResult(0, result);
        }
        //返回结果
        return new PageResult(page.getTotal(), result);
    }

    @Override
    public void updateLabel(Label label) {
        Label newlabel = labelMapper.getLabelById(label.getId());
        newlabel.setLabelName(label.getLabelName());
        newlabel.setUpdateTime(new Date());
        labelMapper.update(newlabel);

    }

    @Override
    public void deleteLabel(Integer id) {
        Label label=labelMapper.getLabelById(id);
        label.setUpdateTime(new Date());
        label.setStatus(0);
        labelMapper.update(label);
    }

    @Override
    public List<Map<String, String>> getLabelNameId(int userId) {
        List<String> labelName = labelMapper.getLabelName(userId);
        List<String> labelIds = labelMapper.getLabelIds(userId);
        List<Map<String, String>> labelInfo =new ArrayList<>();
        for (int i = 0; i < labelName.size()&&i<labelIds.size(); i++) {
            Map<String, String> map = new HashMap<>();
            map.put("id",labelIds.get(i));
            map.put("name",labelName.get(i));
            labelInfo.add(map);

        }
        return labelInfo;

    }

    @Override
    public Label getLabelById(Integer id) {
        return labelMapper.getLabelById(id);
    }

    @Override
    public void insertLabel(LabelDto labelDto) {
        Label label = new Label();
        BeanUtils.copyProperties(labelDto,label);
        label.setStatus(1);
        label.setCreateTime(new Date());
        label.setUpdateTime(new Date());
        labelMapper.insertLabel(label);
    }


}




